import { get, post, IList } from "./index";

export interface IModelPrivate {
    createTime: string;
    id: number;
    name: string;
    isShare: number;
    size: string;
    remark: string;
}

interface ListQuery {
    isShare?: string;
    page: number;
    pageSize: number;
    name: string;
}

// 获取模型列表
export const getModelPrivateList = async (data: ListQuery): Promise<IList<IModelPrivate>> => {
    const res = await post("/model/private/list", data);
    return {
        list: res.data.modelPrivateDtoList || [],
        total: res.data.total || 0,
    };
};

interface PublicParam {
    id: number;
    share: 1 | 2; // 是否发布 1-是 2-否
}

// 发布模型
export const modelPublicShare = async (data: PublicParam) => await post("/model/publicShare", data);

// 创建模型
export const createModel = async (data: { name: string; remark: string }) =>
    await post("/model/create", data);

interface UpdateParam {
    id: number;
    name: string;
    remark: string;
}
// 更新模型
export const updateModel = async (data: UpdateParam) => await post("/model/update", data);

// 删除模型
export const delModel = async (id: number) => await post("/model/delete", { id });

interface DetailListQuery {
    modelId: number;
    page: number;
    pageSize: number;
    parentId: number; // 父文件夹 ID，顶级文件夹为 0
}

export interface DetailItem {
    id: number;
    name: string;
    size: string;
    type: number; // 1-文件 2-文件夹
    updateTime: string;
}
// 模型详情列表
export const getModelFileList = async (data: DetailListQuery): Promise<IList<DetailItem>> => {
    const res = await post("/modelfile/list", data);
    return {
        list: res.data.modelFileListDtoList || [],
        total: res.data.total || 0,
    };
};

interface modelDetail {
    createTime: string;
    id: number;
    name: string;
    size: string;
}

// 获取模型详情信息
export const getModelDetail = async (modelId: number): Promise<modelDetail> => {
    const res = await get("/model/detail", { modelId });
    return res.data || {};
};

// 删除模型文件
export const delModelFiles = async (data: { modelId: number; fileIdList: number[] }) =>
    post("/modelfile/delete", data);

// 模型文件上传
export const uploadModelFiles = async (data: { modelId: number; folder: File[] }) => {
    const formData = new FormData();
    formData.append("modelId", data?.modelId + "");
    for (const file of data?.folder) {
        formData.append("folder", file);
    }

    const res = await post("/modelfile/uploadFolder", formData, {
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
    return res;
};
interface DownlodUrlParam {
    modelId: number;
    fileIdList: number[];
}

// 获取下载文件的url
export const getModelfileDownloadList = async (data: DownlodUrlParam): Promise<string> => {
    const res: any = await post("/modelfile/download", data, {
        responseType: "arraybuffer",
    });
    return res;
};
